const float frequency = 1.4;
const float furriness = 6.0;
const float ambient   = 0.4;

varying vec3 V_eye;
varying vec3 L_eye;
varying vec3 N_eye;
varying vec3 P_obj;

// Returns a 3d grid pattern
float grid(vec3 pos, float freq)
{
   vec3 fp = fract(pos * freq);
   fp *= (1.0 - fp);
   return dot(fp, vec3(1.0, 1.0, 1.0));
}

void main()
{
	//-------------------------------------------------------------------------
	// Coloring	
	//-------------------------------------------------------------------------

	// Create the pattern
	float pattern = 0.5 * grid(P_obj,     frequency);
	pattern      += 0.3 * grid(P_obj, 2.0*frequency);
	pattern      += 0.2 * grid(P_obj, 4.0*frequency);

	//-------------------------------------------------------------------------
	// Fabric style lighting	
	//-------------------------------------------------------------------------

	vec3 V = normalize(V_eye);
	vec3 L = normalize(L_eye);
	vec3 N = normalize(N_eye);

	float diffuse = max(ambient, dot(L, N));

	float cosView = dot(V, N);
	float sinView = 1.0 - cosView * cosView;
	float shine   = pow(sinView, furriness);

	gl_FragColor  = (vec4(1.0, 0.0, 0.0, 1.0)*pattern + shine) * diffuse;
}

